package com.ag.hmk.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbAidgoodsku;
import com.ag.bean.TbAidgoogs;
import com.ag.bean.TbGoods;
import com.ag.bean.TbGoodsstatus;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
/**
 * 
 * @author hmk
 *	查询物资库
 */
public class FindOneAGKUAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private HmkUiltServImpl hmkUiltServImpl;
	private String agkuID1; // 物资库ID
	private String findType1; //查询类别（导向到不同的页面）

	private int page;
	private int maxRow;

	private TbAidgoodsku agkuRST;
	private String goodsSTR;
	
	private PageUtil pageUtil; // 返回仓库中的物品列表

	/*
	 * 查询一条物资库
	 */
	public String findOneAgku() throws Exception {
		TbAidgoodsku agku = null;
		String str1 = "";
		try {
			String hql = "from TbAidgoodsku agku left join fetch agku.tbType left join fetch agku.tbAidgoogses ag "
				+ "left join fetch ag.tbGoodsstatus left join fetch ag.tbGoods g left join fetch g.tbUnit where 1=1";
		System.out.println("----" + agkuID1);
		if (agkuID1 != null && agkuID1.length() > 0) {
			hql += " and agku.a='"+IDUtil.PRESERVE+"'  and agku.aidId =" + agkuID1;
			List result = null;
			result = hmkUiltServImpl.query(hql, 0, 1);
			if (result != null) { //拼接物资的情况
				agku = (TbAidgoodsku) result.iterator().next();
				Set<TbAidgoogs> agSet1 = agku.getTbAidgoogses();
				if (!agSet1.isEmpty()) {
					Iterator<TbAidgoogs> it2 = agSet1.iterator();
					while (it2.hasNext()) {
						TbAidgoogs ag2 = it2.next();
						TbGoodsstatus gs2 = ag2.getTbGoodsstatus();
						TbGoods g2 = ag2.getTbGoods();
						str1 += (g2.getGoodsName() + " " + ag2.getAgNumber()
								+ " " + g2.getTbUnit().getUnitName() + " "
								+ gs2.getGstatusName() + " " + ";<br />");
					}
				} // set为空
				System.out.println(str1);
			} // 结果为空
		} else {
			System.out.println("id不能为空!");
		}
			
		} catch (Exception e) {
			e.printStackTrace();
			agku=null;
			str1="";
		}
		
		this.setAgkuRST(agku);
		this.setGoodsSTR(str1);

		return "findOneAgkuOK";
	}

	/*
	 * 查询物资库基本情况
	 */
	public String findAGKUByID() throws Exception {
		System.out.println("----findAGKUByID----");
		TbAidgoodsku ku1 = null;
		try {
			String hql = "from TbAidgoodsku agku left join fetch agku.tbType  where 1=1";
			System.out.println("----" + agkuID1);
			if (agkuID1 != null && agkuID1.length() > 0) {
				hql += " and agku.a='"+IDUtil.PRESERVE+"'  and agku.aidId = " + agkuID1;
				List result = null;
				result = hmkUiltServImpl.hqlQuery(hql);
				if (result != null) {
					ku1 = (TbAidgoodsku) result.iterator().next();
					System.out.println("---------" + ku1.getAidName());
				}

			} else {
				System.out.println("----------id为空");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ku1=null;
		}
		
		this.setAgkuRST(ku1);
		
		if (findType1!=null && findType1.equals("1")) {
			System.out.println("-gotoEditAidGoods-findtype1-"+findType1);
			return "gotoEditAidGoods";
		} else {
			
			System.out.println("-gotoEditAidGoods-findtype1-"+findType1);
			return "findAGKUByIDOK";
		}
		
	}

	/*
	 * 查询一个救援库中的物品
	 */
	public String findGoods() throws Exception {
		System.out.println("--------findGoods-----");
		PageUtil pUtil = new PageUtil();
		List result = null;
		int total=0;
		try {
			String cql = "from TbAidgoodsku agku  left join fetch agku.tbAidgoogses ag where 1=1 "; //left join fetch agku.tbType
			String hql = "from TbAidgoogs ag left join fetch ag.tbAidgoodsku left join fetch ag.tbGoodsstatus left join fetch " +
					" ag.tbGoods g left join fetch g.tbUnit left join fetch g.tbGoodstype where 1=1";
			System.out.println("--findGoods--" + agkuID1);
			if (agkuID1 != null && agkuID1.length() > 0) {
				cql += " and agku.aidId = " + agkuID1;
				hql += " and ag.tbAidgoodsku.aidId = " + agkuID1;
			}
			cql +="and ag.a='"+IDUtil.PRESERVE+"'";
			hql += " and ag.a='"+IDUtil.PRESERVE+"' order by g.goodsId  desc" ;
				//获取查询结果条数
			List numList  = hmkUiltServImpl.query(cql, 0, 1);
			if (numList != null) {
				 Iterator iterator = numList.iterator();
				 if (iterator.hasNext()) {
					 TbAidgoodsku agku1 = (TbAidgoodsku) iterator.next();
						Set<TbAidgoogs> agSet1 = agku1.getTbAidgoogses();
						total = agSet1.size();
				}
					
			}
		
			System.out.println("------条数:------"+total);
			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
			if (result!=null) {
				System.out.println("----list结果---"+result.size());
			}
		} catch (Exception e) {
			e.printStackTrace();
			//有异常
			total = 0 ;
			result = null;
		}
		
		
		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		
		this.setPageUtil(pUtil);
		
		return "findGoodsOK";
	}

	public TbAidgoodsku getAgkuRST() {
		return agkuRST;
	}

	public void setAgkuRST(TbAidgoodsku agkuRST) {
		this.agkuRST = agkuRST;
	}

	public String getGoodsSTR() {
		return goodsSTR;
	}

	public void setGoodsSTR(String goodsSTR) {
		this.goodsSTR = goodsSTR;
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public void setAgkuID1(String agkuID1) {
		this.agkuID1 = agkuID1;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setFindType1(String findType1) {
		this.findType1 = findType1;
	}

}
